package watchTracker;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SpringLayout.Constraints;
import javax.swing.border.Border;
import javax.swing.plaf.PopupMenuUI;


public class watchGUI extends JFrame {
	private core coreRef;
	private DefaultListModel genreData = new DefaultListModel();
	
	//panels
	private JPanel mainPanel;
	private JPanel titlePanel;
	private JPanel infoPanel1;
	private JPanel infoPanel2;
	private JPanel infoPanel3;
	private JPanel infoPanel4;
	private JPanel filePanel;
	private BoxLayout mainBoxLayout;
	
	//titlePanel - show path
	private JLabel showNameLable;
	
	
	//infoPanel1 - show name, Season Numb, Episode Numb
	private JComboBox showNamesComboBox;
	private JComboBox seasonsComboBox;
	private JComboBox episodeComoboBox;
	private String[] seasonData = {"","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
			"16","17","18","19","20","21","22","23","24","25"};
	private String[] episodeData = {"","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
			"16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33",
			"34","35","36"};
	
	
	//infoPanel2 - date, rating, quality
	private JComboBox dateDayComboBox;
	private JComboBox dateMonthComboBox;
	private JComboBox dateYearComboBox;
	private JComboBox ratingsComboBox;
	private JComboBox qualityComboBox;
	private String[] ratingData = {"","1","2","3","4","5"};
	private String[] dayData = {"","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
			"16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
	private String[] monthData = {"","1 Jan","2 Feb","3 Mar","4 Apr","5 May","6 Jun","7 Jul","8 Aug",
			"9 Sept","10 Oct","11 Nov","12 Dec"};
	private String[] yearData = {"","2010","2009","2008","2007","2006","2005","2004","2003","2002",
			"2001","2000","1999","1998","1997","1996","1995","1994","1993","1992","1991","1990",
			"1989","1988","1987","1986","1985","1984","1983","1982","1981","1980"};
	private String[] qualityData = {"","1","2","3","4","5","6","7","8","9","10"};
	
	
	//infoPanel3 - title
	private JTextField titleTextField;
	
	//infoPanel4 - summary
	private JTextField sumaryTextArea;
	
	
	//filePanel - file List, Buttons
	private JPanel 	filePanel2;
	private BoxLayout horizontalBoxLayout;
	private BoxLayout verticalBoxLayout;
	private JList fileList;
	private JButton updateButton;
	private JButton addShowButton;
	private JButton addGenreButton;
	private JButton watchSelectedButton;
	private DefaultListModel listData;
	
	
	
	
	public watchGUI (core coreRef){
		this.coreRef = coreRef;
		
		//create and add panels
		mainPanel = new JPanel();
		titlePanel = new JPanel();
		infoPanel1 = new JPanel();
		infoPanel2 = new JPanel();
		infoPanel3 = new JPanel();
		infoPanel4 = new JPanel();
		filePanel = new JPanel();
		this.setContentPane(mainPanel);

		
		mainBoxLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
		mainPanel.setLayout(mainBoxLayout);
		mainPanel.add(titlePanel);
		mainPanel.add(infoPanel1);
		mainPanel.add(infoPanel2);
		mainPanel.add(infoPanel3);
		mainPanel.add(infoPanel4);
		mainPanel.add(filePanel);
		
		titlePanel.setMaximumSize(new Dimension(Constants.MAX_WIDTH,100));
		infoPanel1.setMaximumSize(new Dimension(Constants.MAX_WIDTH,100));
		infoPanel2.setMaximumSize(new Dimension(Constants.MAX_WIDTH,100));
		infoPanel3.setMaximumSize(new Dimension(Constants.MAX_WIDTH,100));
		infoPanel4.setMaximumSize(new Dimension(Constants.MAX_WIDTH,100));
		filePanel.setMaximumSize(new Dimension (Constants.MAX_WIDTH,300));
		mainPanel.setBackground(new Color(99030));

		
		//add components to the panels
		//title panel
		showNameLable = new JLabel();
		titlePanel.add(showNameLable);
		
		
		//info panel 1
		showNamesComboBox = new JComboBox();
		seasonsComboBox = new JComboBox(seasonData);
		episodeComoboBox = new JComboBox(episodeData);
		infoPanel1.add(new JLabel("Show Name:"));
		infoPanel1.add(showNamesComboBox);
		infoPanel1.add(new JLabel("Season #:"));
		infoPanel1.add(seasonsComboBox);
		infoPanel1.add(new JLabel("Episode #:"));
		infoPanel1.add(episodeComoboBox);
		
		
		
		//info panel 2
		
		dateDayComboBox = new JComboBox(dayData);
		dateMonthComboBox = new JComboBox(monthData);
		dateYearComboBox = new JComboBox(yearData);
		ratingsComboBox = new JComboBox(ratingData);
		qualityComboBox= new JComboBox(qualityData);
		infoPanel2.add(new JLabel("Air Date (D/M/Y):"));
		infoPanel2.add(dateDayComboBox);
		infoPanel2.add(dateDayComboBox);
		infoPanel2.add(dateMonthComboBox);
		infoPanel2.add(dateYearComboBox);
		infoPanel2.add(new JLabel("Quality:"));
		infoPanel2.add(qualityComboBox);
		infoPanel2.add(new JLabel("Rating:"));
		infoPanel2.add(ratingsComboBox);
		
		
		// info panel 3
		titleTextField = new JTextField(36);
		infoPanel3.add(new JLabel("Title:"));
		infoPanel3.add(titleTextField);
		
		
		// info panel 4
		sumaryTextArea= new JTextField(45);
		
		infoPanel4.add(new JLabel("Summary:"));
		infoPanel4.add(sumaryTextArea);
		
		//file panel
		horizontalBoxLayout = new BoxLayout(filePanel, BoxLayout.X_AXIS);
		filePanel.setLayout(horizontalBoxLayout);
		filePanel2 = new JPanel();
		verticalBoxLayout= new BoxLayout(filePanel2, BoxLayout.Y_AXIS);
		filePanel2.setLayout(verticalBoxLayout);
		
		addShowButton = new JButton("Add Show");
		addShowButton.addActionListener(new MyAction());
		addGenreButton = new JButton("Add Genre");
		addGenreButton.addActionListener(new MyAction());
		updateButton = new JButton("Update");
		updateButton.addActionListener(new MyAction());
		watchSelectedButton = new JButton("Watch Selected");
		watchSelectedButton.addActionListener(new MyAction());
		
		listData = new DefaultListModel();
		fileList = new JList(listData);
		fileList.setMinimumSize(new Dimension(300,200));

		
		filePanel.add(fileList);
		filePanel.add(filePanel2);
		filePanel2.add(updateButton);
		filePanel2.add(addShowButton);
		filePanel2.add(addGenreButton);
		filePanel2.add(watchSelectedButton);
		
		this.setSize(Constants.MAX_WIDTH,500);
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);

	}

	public void addShowName(String name){
		showNamesComboBox.addItem(name);
	}
	public void addGenre(String name){
		genreData.addElement(name);
	}
	public void addFile(String fileName){
		listData.addElement(fileName);
		fileList.setModel(listData);
	}
	public void setSeason(int seasonNumb){
		seasonsComboBox.setSelectedIndex(seasonNumb);
	}
	public void setEpisode(int episodeNumb){
		episodeComoboBox.setSelectedIndex(episodeNumb);
	}
	public void setEpisodeTitle(String episodeTitle){
		titleTextField.setText(episodeTitle);
	}
	public void setShowName(String showName){
		showNamesComboBox.setSelectedItem(showName);
	}
	public void setSelectedFile(int index){
		fileList.setSelectedIndex(index);
	}
	public void diplayFilePath(String filePath){
		showNameLable.setText(filePath);
		this.setTitle(filePath + " - " + Constants.WINDOW_TITLE);	
	}
	
	public void reset(){
		showNameLable.setText("");
		this.setTitle(Constants.WINDOW_TITLE);
		listData = new DefaultListModel();
		fileList.setModel(listData);
	}
	
	public class MyAction implements ActionListener{
	    public void actionPerformed(ActionEvent e){
	      String text = (String)e.getActionCommand();
	      
	      if(text == "Add Show"){
	    	  Object response = JOptionPane.showInputDialog(titlePanel,
	              "Enter The Name Of The Show",
	             "Enter The Name Of The Show", JOptionPane.OK_CANCEL_OPTION
	              );
	    	  String tempName = response.toString();
	    	  String s = (String)JOptionPane.showInputDialog(
	                    mainPanel,
	                    "Choose Genre for"+tempName+":",
	                    "Customized Dialog",
	                    JOptionPane.PLAIN_MESSAGE,
	                    null,
	                    genreData.toArray(),
	                    "ham");

	    	  
	    	  addShowName(response.toString());
	    	  coreRef.addShowDB(tempName, s);
			
	      } else if (text == "Watch Selected"){
	    	  
	    	  coreRef.watchShow(fileList.getSelectedValue().toString());
	      } else if (text == "Add Genre"){
	    	  Object response = JOptionPane.showInputDialog(titlePanel,
		              "Enter The Name Of The Genre",
		              "Enter The Name Of The Genre", JOptionPane.OK_CANCEL_OPTION
		              );
		    	  addGenre(response.toString());
		    	  coreRef.addGenreDB(response.toString());
	      }else if (text =="Update"){
	    	  coreRef.addEpisodeDB(showNamesComboBox.getSelectedItem().toString(), titleTextField.getText(), 
	    			  seasonsComboBox.getSelectedIndex(), episodeComoboBox.getSelectedIndex(), 
	    			  qualityComboBox.getSelectedIndex(), ratingsComboBox.getSelectedIndex(),sumaryTextArea.getText(),
	    			  showNameLable.getText());
	    	  
	      }
	    }
	}
}
